package com.viewshine.nettydemo.springBeanMethod.controller;

import com.viewshine.nettydemo.springBeanMethod.config.SpringContextUtil;
import com.viewshine.nettydemo.springBeanMethod.config.property.NettyServerConnectObj;
import com.viewshine.nettydemo.springBeanMethod.nettyClient.SpringEchoClient;
import com.viewshine.nettydemo.springBeanMethod.nettyVo.NettyRequest;
import com.viewshine.nettydemo.springBeanMethod.nettyVo.NettyRespose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName NettyTestController
 * @Author rhb
 * @Description 测试API
 *                  SpringEchoClient 虽然声明为多例bean
 *                  但通过@Autowired注解注入的springEchoClient 在整个NettyTestController整个方法获取的实例均为同一对象
 *                  而通过SpringContextUtil方法（即通过Spring上下创建的Bean）会在每次调用时都会创建一个新的Bean（即使同一方法也会获取到新的Bean）
 * @Date 2019-01-18 11:22
 * @Version 1.0
 **/
@RestController
@Slf4j
public class NettyTestController {

    @Autowired
    private NettyServerConnectObj nettyServerConnectObj;

    @Autowired
    private SpringEchoClient springEchoClient;

    @RequestMapping("test1")
    public NettyRespose teat1() {
        NettyRespose nettyRespose = null;
        try {
            NettyRequest request = new NettyRequest();
            request.setSendData("20190118abcdefj");

            log.error("@Autowired...test1...springEchoClient="+springEchoClient);

            springEchoClient.setNettyRequest(request);
            springEchoClient.send();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nettyRespose = springEchoClient.getNettyRespose();
        }
        return nettyRespose;
    }


    @RequestMapping("test2")
    public NettyRespose teat2() {
        NettyRespose nettyRespose = null;
        try {
            NettyRequest request = new NettyRequest();
            request.setSendData("中文20190118该吃药了");

            log.error("@Autowired...teat2...springEchoClient="+springEchoClient);

            springEchoClient.setNettyRequest(request);
            springEchoClient.send();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nettyRespose = springEchoClient.getNettyRespose();
        }
        return nettyRespose;
    }


    @RequestMapping("test3")
    public NettyRespose teat3() {
        NettyRespose nettyRespose = null;
        try {
            NettyRequest request = new NettyRequest();
            request.setSendData("SpringContextUtil..............1");

            SpringEchoClient bean = SpringContextUtil.getBean(SpringEchoClient.class);

            log.error("@Autowired...test3...springEchoClient[bean]="+bean);

            bean.setNettyRequest(request);
            bean.send();
            nettyRespose = bean.getNettyRespose();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return nettyRespose;
    }



    @RequestMapping("test4")
    public NettyRespose teat4() {
        NettyRespose nettyRespose = null;
        try {
            NettyRequest request = new NettyRequest();
            request.setSendData("SpringContextUtil..............再试一次");

            SpringEchoClient bean = SpringContextUtil.getBean(SpringEchoClient.class);

            log.error("@Autowired...test4...springEchoClient[bean]="+bean);
            bean.setNettyRequest(request);
            bean.send();
            nettyRespose = bean.getNettyRespose();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return nettyRespose;
    }

}
